<?php
/**
 * 文件上传类
 * 2012-05-25 修改成多文件上传
 * 
 * @file Upload.class.php
 * @create 2012-05-25
 * @version v2.0
 * @author veapon 
 * @copyright (C) veapon.com
 */

class Upload{
	private $file = "";
	private $error = false; 
	private $errMsg = array(
				'illegal_op'	=> '非法上传',
				'size_error'	=> '文件大小超过限制',
				'type_error'	=> '文件类型不允许上传',
				'override_error'=> '已存在同名文件',
				'make_dir_error'=> '创建保存目录失败',
			); 
	public $cfg = array(
				'max_size'	=> 0,
				'safe_ext'	=> array('jpg','png','gif','txt','doc','docx','ppt','pptx','xls','xlsx','pdf','rar','zip','7z'),
				'override'	=> true,
				'save_path'	=> './',
			);
	public $saveName;
	// array $uploaded since v2.0
	private $uploaded = array();


	public function __construct($field="file", $config=""){
		$this->file = $_FILES[$field];
		// 将单文件上传处理成多文件上传
		if( !is_array($this->file['tmp_name']) ){
			foreach($this->file as $k=>$v){
				unset($this->file[$k]);
				$this->file[$k][] = $v;
			}
		}
		if( is_array($config) ) $this->cfg = array_merge($this->cfg,$config);
	}
	
	private function valid(){
		foreach( $this->file['tmp_name'] as $k=>$v ){
			if( empty($v) ) continue;
			if( $this->file['error'][$k] !== UPLOAD_ERR_OK ){
				$this->error = '上传错误，错误代码：'.$this->file['error'][$k];
				return false;
			}
			if( !is_uploaded_file($v) ){
				$this->error = $this->errMsg['illegal_op'];
				return false;
			}
			if( $this->cfg['safe_ext'] && count($this->cfg['safe_ext'])>0 && !in_array($this->get_ext($this->file['name'][$k]),$this->cfg['safe_ext']) ){
				$this->error = $this->errMsg['type_error'];
				return false;
			}
			if( !empty($this->cfg['max_size']) && ($this->file['size'][$k]>$this->cfg['max_size']) ){
				$this->error = $this->errMsg['size_error'];
				return false;
			}
		}
		return true;
	}

	private function create_folder($path, $mode=0777) { 
		if (!file_exists($path)) { 
			$this->create_folder(dirname($path)); 
			@mkdir($path, $mode); 
		} 
	} 

	public function save($prefix="", $fixed=false){
		//$this->saveName = empty($file_name) ? $this->file['name'] : $file_name.'.'.$this->get_pro('ext');
		if( $this->valid() ){
			$return = array();
			foreach( $this->file['tmp_name'] as $k=>$v ){
				if( !empty($this->cfg['save_path']) && $this->cfg['save_path'] != './' && !is_dir($this->cfg['save_path']) ){
					$this->create_folder($this->cfg['save_path']);
				}
				$this->saveName = $prefix.time().'.'.$this->get_ext($this->file['name'][$k]);
				$file = ltrim($this->cfg['save_path'],DS).DS.$this->saveName;
				if( move_uploaded_file($v,$file) ){
					$return[] =  array('name'=>$this->file['name'][$k], 'saveName'=>$this->saveName);

				}
			}
			if( count($return)>0 ){
				$this->uploaded = $return;
				return json_encode($return);
			}
			return false;
		}else{
			return false;
		}
	}

	//public function get_pro($item){
	//	switch($item){
	//		case "ext":
	//			
	//		break;
	//		case "size":
	//			return $this->file['size'];
	//		break;
	//		default:
	//			return false;
	//		break;
	//	}
	//}

	/**
	 * 取得扩展名
	 * 
	 * @param $fileName
	 * @return 
	 * @since v2.0
	 * @create 2012-05-25
	 */
	public function get_ext($fileName){
		$filter = explode(".",$fileName);
		return strtolower($filter[count($filter)-1]);
	}

	public function get($param){
		if( isset($this->$param) ) return $this->$param;
	}

	public function config($array){
		if( is_array($array) && count(array_diff_key($array,$this->cfg))==0 ){
			$this->cfg = array_merge($this->cfg,$array);
		}
	}
}
